//---------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#pragma hdrstop
#include "bibliojogo.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

void preenche(char m[][DIM], int nlinhas, int ncolunas)
        {
        int i, j;
        for (i=0;i<nlinhas;i++)
                for (j=0;j<ncolunas;j++)
                m[i][j]='.';
        }

void preenchefim(char m[][DIM], char a[][DIM], char b[][DIM], int nlinhas, int ncolunas)
        {
        int i, j;
        for (i=0;i<nlinhas;i++)
                for (j=0;j<ncolunas;j++)
                        if (a[i][j]<'5' && a[i][j]>'0' || a[i][j]=='a')
                                if (a[i][j]=='a')
                                        m[i][j]='a';
                                else
                                        m[i][j]='*';
                        else
                                if (b[i][j]<'5' && b[i][j]>'0')
                                        m[i][j]='X';
                                else
                                        m[i][j]=b[i][j];
        }

void mostra_tabuleiro(char m[][DIM], int nlinhas, int ncolunas)
        {
        int i, j;
        printf("\n\n     A  B  C  D  E  F  G  H  I  J\n\n");
        for (i=0;i<nlinhas;i++)
                {
                printf(" %d  ",i+1);
                for (j=0;j<ncolunas;j++)
                        {
                        printf(" %c ",m[i][j]);
                        }
                printf("\n\n");
                }
        }

void colocanavios(char m[][DIM], int dimbarco)
        {
        int linha, coluna, orientacao,i,n=0;
        char nr;
        nr=(char)dimbarco+48;
        srand(time(0));
        while (1)
                {
                linha=rand()%10;
                coluna=rand()%10;
                orientacao=rand()%2;
                if (orientacao<1)
                        {
                        if((linha+dimbarco)>9 || !valida(m,linha,coluna,'v',dimbarco))
                                continue;
                        else
                                {

                                for(i=0;i<dimbarco;i++)
                                        {
                                        m[linha+i][coluna]=nr;
                                        }
                                }
                        }
                else
                        {
                        if((coluna+dimbarco)>9 || !valida(m,linha,coluna,'h',dimbarco))
                                continue;
                        else
                                {

                                for(i=0;i<dimbarco;i++)
                                        {
                                        m[linha][coluna+i]=nr;
                                        }
                                }
                        }
                break;
                }
        }

int valida(char m[][DIM], int linha, int coluna, char direccao, int dim_barco)
        {
        int i, j;
        if(direccao=='v')
                {
                for(i=-1; i<dim_barco+1; i++)
                        for(j=-1; j<2; j++)
                                if(m[linha+i][coluna+j]=='X' || m[linha+i][coluna+j]=='1' || m[linha+i][coluna+j]=='2' || m[linha+i][coluna+j]=='3' || m[linha+i][coluna+j]=='4')
                                        return 0;
                }
        else
                {
                for(i=-1; i<2; i++)
                        for(j=-1; j<dim_barco+1; j++)
                                if(m[linha+i][coluna+j]=='X' || m[linha+i][coluna+j]=='1' || m[linha+i][coluna+j]=='2' || m[linha+i][coluna+j]=='3' || m[linha+i][coluna+j]=='4')
                                        return 0;
                }
        return 1;
        }

void ganhou()
        {
        clrscr();
        caixa (79,24,'#',' ');
        gotoxy(55,9);
        printf("       ~~~~~~");
        gotoxy(55,10);
        printf("   ________");
        gotoxy(55,11);
        printf("  /________\\");
        gotoxy(55,12);
        printf(" ~~~~~~~~~~~~");
        gotoxy(20,13);
        printf("   ___|__|__________       ~~~~");
        gotoxy(20,14);
        printf("~~~~~~~~~~~~~~~~~~~~~         ~~~~~");
        gotoxy(36,16);
        printf("            __________    ~~~~");
        gotoxy(36,17);
        printf("         \\_|_:_:_:_:__\\");
        gotoxy(36,18);
        printf("    _____|____________||_______   ~~~~");
        gotoxy(36,19);
        printf("    \\ o   o   o   o   o   o   /");
        gotoxy(36,20);
        printf("     \\                       /");
        gotoxy(36,21);
        printf("~~~~~~\\_____________________/~~~~~");
        gotoxy(17,5);
        printf("* * *  P A R A B E N S !  G A N H O U !   * * *");
        gotoxy(17,6);
        printf("       --------------------------------");
        getch();
}


